﻿// -----------------------------------------------------------------------
// <copyright file="FormSmooth.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.BaseForm
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text;

    /// <summary>
    /// Used to make forms with rounded corners
    /// </summary>
    internal class FormSmooth
    {
        /// <summary>SetWindow region API</summary>
        /// <param name="hWnd">Handler pointer</param>
        /// <param name="hRgn">Pointer region</param>
        /// <param name="bRedraw">Redraw the form</param>
        /// <returns>Window region</returns>
        [DllImport("user32.dll")]
        internal static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

        /// <summary>CreateRoundRectangular region API</summary>
        /// <param name="x1">X1 Form value</param>
        /// <param name="y1">Y1 Form value</param>
        /// <param name="x2">X2 Form value</param>
        /// <param name="y2">Y2 Form value</param>
        /// <param name="cx">CX Form value</param>
        /// <param name="cy">CY Form value</param>
        /// <returns>Window region</returns>
        [DllImport("gdi32.dll")]
        internal static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int cx, int cy);

        /// <summary>DeleteObject API</summary>
        /// <param name="hObject">Delete this object</param>
        /// <returns>Returns something...</returns>
        [DllImport("gdi32.dll")]
        internal static extern bool DeleteObject(IntPtr hObject);

        /// <summary>Make the form smooth</summary>
        /// <param name="frm">The form to be rounded</param>
        /// <param name="curvature">Rounded corner value</param>
        internal static void SmoothForm(ref System.Windows.Forms.Form frm, int curvature)
        {
            try
            {
                IntPtr hRgn;
                int x1, y1;

                x1 = frm.Width / 1;
                y1 = frm.Height / 1;
                hRgn = CreateRoundRectRgn(0, 0, x1, y1, curvature, curvature);
                SetWindowRgn(frm.Handle, hRgn, true);
                DeleteObject(hRgn);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>Make the form smooth</summary>
        /// <param name="baseForm">The form to be rounded</param>
        /// <param name="curvature">Rounded corner value</param>
        internal static void SmoothForm(System.Windows.Forms.Form baseForm, int curvature)
        {
            try
            {
                IntPtr hRgn;
                int x1, y1;

                x1 = baseForm.Width / 1;
                y1 = baseForm.Height / 1;
                hRgn = CreateRoundRectRgn(0, 0, x1, y1, curvature, curvature);
                SetWindowRgn(baseForm.Handle, hRgn, true);
                DeleteObject(hRgn);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
